#include "GameEnemy.h"
#include "GameSettings.h"

using namespace cocos2d;

GameEnemy::GameEnemy(void)
{
}

GameEnemy::~GameEnemy(void)
{
}

bool GameEnemy::initAtPositionInLayer(CCPoint pos, GameLayer *layer) {
	CCSpriteFrame *spriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("enemy1.png");
	if (this->initWithSpriteFrameName("enemy1.png")) {
		speed = 3;
		srand(123123);
		act = (enemyAct)(rand()%2 + 1);
		this->setPosition(pos);
		moneyLabel = CCLabelTTF::create("100", "Marker Felt", 16);
		moneyLabel->setColor(ccc3(0, 255, 0));
		layer->addChild(moneyLabel);
		moneyLabel->setVisible(false);
		layer->addChild(this);

		CCSpriteFrame *frame1 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("enemy1.png");
		CCSpriteFrame *frame2 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("enemy2.png");
		CCArray *frameArray = CCArray::arrayWithObjects(frame1, frame2, NULL);

		CCAnimation *animation = CCAnimation::animationWithSpriteFrames(frameArray, 0.2);
		CCAnimate *animate = CCAnimate::actionWithAnimation(animation);
		this->runAction(CCRepeatForever::actionWithAction(animate));
		//CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(this, callfunc_selector(this, GameEnemy::enemyMove));
		this->schedule(schedule_selector(GameEnemy::enemyMove), 0.1f);

		this->layer = layer;
		this->tileArray = layer->tileArray;
		layer->retain();
		tileArray->retain();
	}
	return true;
}


void GameEnemy::enemyMove(float f) {
	if (act == kHorizon) {
		CCSprite *tile;
		bool leftnEnable = false;
		bool rightnEnable = false;
		if (this->getPosition().x == 1.5*TileWidth) {
			leftnEnable = true;
		}
		if (this->getPosition().x == 13.5*TileWidth) {
			rightnEnable = true;
		}
		for (int i=0; i<this->tileArray->count(); i++) {
			tile = (CCSprite *)this->tileArray->objectAtIndex(i);
			if (tile->getPosition().y == this->getPosition().y && this->getPosition().x-tile->getPosition().x==TileWidth) {
				leftnEnable = true;
			}
			if (tile->getPosition().y == this->getPosition().y && tile->getPosition().x-this->getPosition().x==TileWidth) {
				rightnEnable = true;
			}
		}
		if (leftnEnable&&rightnEnable) {
			act = kVertical;
			return;
		}

		for (int i=0; i<tileArray->count(); i++) {
			tile = (CCSprite *)tileArray->objectAtIndex(i); 
			float x = this->getPosition().x;
			float y = this->getPosition().y;
			float tile_x = tile->getPosition().x;
			float tile_y = tile->getPosition().y;
			if ((x<tile_x && tile_x-x<=TileWidth && fabs(tile_y-y)<TileWidth && speed>0)) {
				speed = -speed;
				break;
			}
			if (x >= 13.5*TileWidth) {
				this->setPosition(ccp(13.5*TileWidth, y));
				speed = -speed;
				break;
			}
			if (x>tile_x && x-tile_x<=TileWidth && fabs(tile_y-y)<TileWidth && speed<0) {
				speed = -speed;
				break;
			}
			if (x <= 1.5*TileWidth) {
				this->setPosition(ccp(1.5*TileWidth, y));
				speed = -speed;
				break;
			}

		}
		this->setPosition(ccp(this->getPosition().x+speed, this->getPosition().y));
		stepCount++;
	}

	if (act == kVertical) {
		CCSprite *tile;
		bool upnEnable = false;
		bool downnEnable = false;
		float x = this->getPosition().x;
		float y = this->getPosition().y;
		if (y == 0.5*TileHeight) {
			downnEnable = true;
		}
		if (y == 8.5*TileHeight) {
			upnEnable = true;
		}
		for (int i = 0; i<tileArray->count(); i++) {
			tile = (CCSprite *)tileArray->objectAtIndex(i);
			float tile_x = tile->getPosition().x;
			float tile_y = tile->getPosition().y;
			if (tile_x == x && y-tile_y == TileHeight) {
				downnEnable = true;
			}
			if (tile_x == x && tile_y - y == TileHeight) {
				upnEnable = true;
			}
		}
		if (downnEnable && upnEnable) {
			act = kHorizon;
			return;
		}

		for (int i = 0; i<tileArray->count(); i++) {
			tile = (CCSprite *)tileArray->objectAtIndex(i);
			float tile_y = tile->getPosition().y;
			float tile_x = tile->getPosition().x;
			if (y<tile_y && tile_y-y<=TileHeight && fabs(tile_x - x)<TileWidth) {
				speed = -speed;
				break;
			}
			if (y >= 8.5*TileHeight) {
				this->setPosition(ccp(x, 8.5*TileHeight));
				speed = -speed;
				break;
			}
			if (y>tile_y && y-tile_y<=TileHeight && fabs(tile_x-x)<TileWidth) {
				speed = -speed;
				break;
			}
			if (y<=0.5*TileHeight) {
				this->setPosition(ccp(x, 0.5*TileHeight));
				speed = -speed;
				break;
			}
		}
		this->setPosition(ccp(x, y+speed));
		stepCount++;
	}

	if (stepCount>=64/Nandu) {
		if ((int)(this->getPosition().x+0.5*TileWidth)%TileWidth == 0 && act==kHorizon) {
			act = kVertical;
			stepCount = 0;
		} else if ((int)(this->getPosition().y+0.5*TileHeight)%TileHeight == 0 && act==kVertical) {
			act = kHorizon;
			stepCount = 0;
		}
	}
}


void GameEnemy::enemyKill() {
	CCScaleTo *scale = CCScaleTo::actionWithDuration(1, 0);
	CCTintTo *tint = CCTintTo::actionWithDuration(1, 255, 255, 255);
	this->runAction(tint);
	CCFiniteTimeAction *seq = CCSequence::actions(scale, CCCallFunc::actionWithTarget(this, callfunc_selector(GameEnemy::removeEnemy)));
	this->runAction(seq);

	moneyLabel->setVisible(true);
	moneyLabel->setPosition(this->getPosition());

	CCScaleTo *scaleto = CCScaleTo::actionWithDuration(1, 2);
	CCMoveBy *moveby = CCMoveBy::actionWithDuration(1, ccp(0, 32));
	moneyLabel->runAction(scaleto);
	moneyLabel->runAction(moveby);
}

void GameEnemy::removeEnemy() {
	moneyLabel->removeFromParentAndCleanup(true);
	this->removeFromParentAndCleanup(true);
}

